package com.zc.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zc.edu.entity.Teacher;
import com.zc.edu.entity.vo.TeacherQuery;
import com.zc.edu.service.TeacherService;
import com.zc.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-11-22
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduService/teacher")
@CrossOrigin
public class TeacherController {
	@Autowired
	private TeacherService teacherService;

	// 查询讲师表所有数据
	@ApiOperation("查询所有讲师")
	@GetMapping("/findAll")
    public R findAllTeacher() {
		List<Teacher> list = teacherService.list(null);
		R r = R.ok().data("data", list);
		return r;
	}

	// 逻辑删除讲师
	@ApiOperation("根据id删除指定讲师")
	@DeleteMapping("/removeTeacher/{id}")
	public R removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable("id")String id) {
		return teacherService.removeById(id) ? R.ok() : R.error();
	}

	// 分页查询讲师
	@ApiOperation("分页查询讲师")
	@GetMapping("/pageTeacher/{currentPage}/{size}")
	public R pageListTeacher(@ApiParam(name = "currentPage", value = "当前页")@PathVariable("currentPage")long currentPage,
	                         @ApiParam(name = "size", value = "每页记录数")@PathVariable("size")long size) {
		Page<Teacher> pageTeacher = new Page<>(currentPage, size);
		teacherService.page(pageTeacher, null);
		long total = pageTeacher.getTotal();
		List<Teacher> records = pageTeacher.getRecords();
		return R.ok().data("total", total).data("records", records);
	}

	@ApiOperation("带条件的分页查询讲师")
	@PostMapping("/pageListTeacherCondition/{currentPage}/{size}")
	public R pageListTeacherCondition(@ApiParam(name = "currentPage", value = "当前页")@PathVariable("currentPage")long currentPage,
	                                  @ApiParam(name = "size", value = "每页记录数")@PathVariable("size")long size,
	                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
		Page<Teacher> pageTeacher = new Page<>(currentPage, size);
		QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
		String name = teacherQuery.getName();
		Integer level = teacherQuery.getLevel();
		String begin = teacherQuery.getBegin();
		String end = teacherQuery.getEnd();
		wrapper.like(name != null, "name", name)
				.eq(level != null, "level", level)
				.ge(begin != null, "gmt_create", begin)
				.le(end != null, "gmt_create", end);
		wrapper.orderByDesc("gmt_create");
		teacherService.page(pageTeacher, wrapper);
		long total = pageTeacher.getTotal();
		List<Teacher> records = pageTeacher.getRecords();
		return R.ok().data("total", total).data("records", records);
	}

	@ApiOperation("添加讲师")
	@PostMapping("/addTeacher")
	public R addTeacher(@RequestBody Teacher teacher) {
		boolean save = teacherService.save(teacher);
		return save ? R.ok() : R.error();
	}

	@ApiOperation("根据id查询讲师")
	@GetMapping("/getTeacherById/{id}")
	public R getTeacherById(@ApiParam(name = "id", value = "讲师id", required = true)
                            @PathVariable String id) {
		Teacher teacher = teacherService.getById(id);
		return R.ok().data("teacher", teacher);
	}

	@ApiOperation("修改讲师")
	@PostMapping("/updateTeacher")
	public R updateTeacher(@RequestBody Teacher teacher) {
		boolean update = teacherService.updateById(teacher);
		return update ? R.ok() : R.error();
	}
}

